package com.fenghongzhang.day014;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenghongzhang.day014.db.SearchDao;
import com.fenghongzhang.day014.util.DaoManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ScrollView scrollView;
    private LinearLayout ll;
    private EditText edit;
    private ImageView search;
    private MyRecycle myRecycle;
    private TextView showAll;
    private  List<Search> list;
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrollView = (ScrollView) findViewById(R.id.scrollView);
        ll = (LinearLayout) findViewById(R.id.ll);
        edit = (EditText) findViewById(R.id.edit);
        search = (ImageView) findViewById(R.id.search);
        myRecycle = (MyRecycle) findViewById(R.id.myRecycle);
        showAll = (TextView) findViewById(R.id.showAll);


        list = new ArrayList<>();

//        Search search = new Search();
//        search.setName("哈哈哈");
//
//        list.add(search);
//        list.add(search);
//        list.add(search);
//        list.add(search);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        myRecycle.setLayoutManager(linearLayoutManager);

        //从数据库先查询一次
        SearchDao searchDao = DaoManager.getInstance().getDaoSession().getSearchDao();
        List<Search> list1 = searchDao.queryBuilder().orderDesc(SearchDao.Properties.Id).limit(10).build().list();
        list.addAll(list1);
        myAdapter = new MyAdapter(R.layout.rv_layout, list);
        myRecycle.setAdapter(myAdapter);


        myAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Search item = myAdapter.getItem(position);

                //1
                list.remove(item);

                //2
                DaoManager.getInstance().getDaoSession().getSearchDao().delete(item);

                //3
                myAdapter.notifyDataSetChanged();
            }
        });

    }

    public void add(View view) {

        String string = edit.getText().toString();
        Search search = new Search();
        search.setName(string);
//        search.setId(1l);


        //1,到集合
//        list.add(search);
        //排序
//        Collections.sort(list, new Comparator<Search>() {
//            @Override
//            public int compare(Search o1, Search o2) { // 0 >0 <0
//                return (int) (o2.getId() - o1.getId());
//            }
//        });




        list.clear();

        //2,到数据库
        SearchDao searchDao = DaoManager.getInstance().getDaoSession().getSearchDao();
        searchDao.insert(search);
        List<Search> list1 = searchDao.queryBuilder().orderDesc(SearchDao.Properties.Id).limit(10).build().list();

        list.addAll(list1);

        //适配器刷新一下
        myAdapter.notifyDataSetChanged();

    }

    public void show(View view) {
        if(myRecycle.isFull()){
            //
            showAll.setText("展开所有");
            myRecycle.setFull(false);

            new AlertDialog.Builder(this).setTitle("删除").setMessage("确定吗?")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            DaoManager.getInstance().getDaoSession().getSearchDao().deleteAll();

                            list.clear();
                            myAdapter.notifyDataSetChanged();
                        }
                    }).show();

        }else{
            showAll.setText("删除所有");
            myRecycle.setFull(true);
        }
    }
}
